package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.alimaplibrary.MapLocationTools;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGasServiceBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.GasListInfoEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.transport.GasListAdapter;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.viewmodel.transport.GasServiceModule;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 *加油服务列表
 */
public class GasServiceActivity extends AppActivity {
    ActivityGasServiceBinding binding;
    private List<GoodsInfoTypeEntity3> brandList;

    private List<GoodsInfoTypeEntity3> typeList;
    private List<GoodsInfoTypeEntity3> priceList;

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private GoodsTypePopWindow3 brandPopWindow;
    private GoodsTypePopWindow3 typePopWindow;
    private GoodsTypePopWindow3 pricePopWindow;
    GasListAdapter adapter;
    @Inject
    GasServiceModule module;
    private int position;
    private int position2;
    private int position3;
    private MapLocationTools mapLocationTools;

    @Override
    protected void initView() {
        binding= DataBindingUtil.setContentView(this, R.layout.activity_gas_service);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.rightView.setOnClickListener((View)->{
            Intent intent=new Intent(getActivity(),GasMapActivity.class);
            startActivity(intent);

        });
        binding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration(getResources().getDimension(R.dimen.dp5)));
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                module.getFilter(false);

            }

            @Override
            public void onLoadMore() {
              module.getFilter(true);
            }
        });
        adapter=new GasListAdapter(this,null);
        binding.recyclerView.setAdapter(adapter);
        //区域popWindow
        sendGoodsLocationPopWindow3 = new SendGoodsLocationPopWindow3(getActivity());
        sendGoodsLocationPopWindow3.setBgView(binding.grayLayout);
        sendGoodsLocationPopWindow3.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvPlace.setText(" "+entity.getCityName()+"");
                module.infoEntity.setCityNo(entity.getCityNo());
                module.getFilter(false);


            }
        });
        binding.locationLayout.setOnClickListener(this);
        binding.priceLayout.setOnClickListener(this);
        binding.gasBrancLayout.setOnClickListener(this);
        binding.gasTypeLayout.setOnClickListener(this);


        brandPopWindow = new GoodsTypePopWindow3(getActivity());
        brandPopWindow.setBgView(binding.grayLayout);
        brandPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)type;
                List<String> brandList=new ArrayList<>();
                position= (int) key2;
                binding.tvBrand.setText(entity.getName());
                brandList.add(entity.getNo());
                 module.infoEntity.setBrandNos(brandList);
                module.getFilter(false);
            }
        });
        typePopWindow = new GoodsTypePopWindow3(getActivity());
        typePopWindow.setBgView(binding.grayLayout);
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)type;

                position2= (int) key2;
                binding.tvType.setText(entity.getName());

                module.infoEntity.setOilTypeNo(entity.getNo());
                module.getFilter(false);
            }
        });
        pricePopWindow = new GoodsTypePopWindow3(getActivity());
        pricePopWindow.setBgView(binding.grayLayout);
        pricePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)type;

                position3= (int) key2;
                binding.tvPrice.setText(entity.getName());
                if(entity.getName().equals("价格优先")){
                    module.infoEntity.setPriceSort(1);
                }else{
                    module.infoEntity.setPriceSort(2);
                }

                module.getFilter(false);
            }
        });
        adapter.setOnItemClick(new AdapterOnItemClick<GasListInfoEntity>() {
            @Override
            public void onItemClick(GasListInfoEntity gasListInfoEntity, int position) {
                module.getH5Url(gasListInfoEntity.getNo(),gasListInfoEntity.getOilModelNo());

            }
        });

    }

    @Override
    protected void init() {
        startLocation();
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.price_layout:
                if(priceList!=null&&priceList.size()>0){
                    showListPop();
                }else{
                    module.getPriceType();
                }
                break;
            case R.id.location_layout:
                setOptionStatus(2);
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showSendPopWindow();
                } else {
                    module.getAddressModel();
                }
                break;
            case R.id.gas_type_layout:
                if(typeList!=null&&typeList.size()>0){
                    showTypePop();

                }else{
                    module.oilType();
                }
                break;
            case R.id.gas_branc_layout:
                if(brandList!=null&&brandList.size()>0){
                    showBrandsPop();
                }else{
                   module.oilBrand();
                }
                break;
        }
    }

    //油品牌
    public void setBrandPop(List<GoodsInfoTypeEntity3> brandList) {
        this.brandList = brandList;
        showBrandsPop();

    }
    private void showBrandsPop(){
        brandPopWindow.showPopwindow(binding.line,0,0);
        brandPopWindow.setData(brandList,position);
    }

    public void setType(List<GoodsInfoTypeEntity3> brandList) {
        this.priceList = brandList;
        showListPop();

    }
    private void showListPop(){
        pricePopWindow.showPopwindow(binding.line,0,0);
        pricePopWindow.setData(priceList,position3);
    }

    //油类型
    public void setTypePop(List<GoodsInfoTypeEntity3> typeList) {
        this.typeList = typeList;
        binding.tvType.setText(typeList.get(0).getName());

        module.infoEntity.setOilTypeNo(typeList.get(0).getNo());
        module.getFilter(false);
       // showTypePop();
    }
    private void showTypePop(){
        typePopWindow.showPopwindow(binding.line,0,0);
        typePopWindow.setData(typeList,position2);
    }
    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        initJsonData();
    }

    private void initJsonData() {

        for (int i = 0; i < addressEntityList.size(); i++) {

//            cityList = new ArrayList<>();
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        showSendPopWindow();
    }
    private void showSendPopWindow() {
        sendGoodsLocationPopWindow3.showPopwindow(binding.line, 0, 0);
        sendGoodsLocationPopWindow3.setData(addressEntityList);
    }
    private void setOptionStatus(int index) {
        switch (index) {
            case 1:
                binding.tvPrice.setSelected(true);
                binding.sendGoodsIv.setSelected(true);
                break;
            case 2:
                binding.tvPlace.setSelected(true);
                binding.purposeGoodsIv.setSelected(true);
                break;
            case 3:
                binding.tvType.setSelected(true);
                binding.typeIv.setSelected(true);
                break;
            case 4:
                binding.tvBrand.setSelected(true);
                binding.brandIv.setSelected(true);
                break;
        }
    }
    public void getList(List<GasListInfoEntity> list,boolean isLoadMore){
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() > 0) {
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }
    /**
     * 开始定位
     */
    private void startLocation() {
        if (mapLocationTools == null) {
            mapLocationTools = new MapLocationTools(getApplicationContext());
            mapLocationTools.initLocation(false);
        }
        mapLocationTools.setLocationListener(new MapLocationTools.LocationListener() {
            @Override
            public void getLocationListener(String address, Double Longitude, Double Latitude) {
                module.infoEntity.setLatitude(Latitude);
                module.infoEntity.setLongitude(Longitude);
                module.oilType();

            }
        });
        mapLocationTools.startLocation();
    }

}
